package org.eclipse.football.web;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class ReadDataFromURL {

	/**
	 * reads the data from local file created by GetDataFromURL
	 * 
	 * @param stat
	 *            : rushing, receiving, passing, kicking
	 * @param week
	 *            what week data are we reading
	 * @throws IOException
	 */
	public static void readFile(String stat, String week) throws IOException {
		// System.out.println(stat + week + ".txt");
		File inputFile = new File(stat + week + ".txt");
		FileReader inputFileReader = null;
		try {
			inputFileReader = new FileReader(inputFile);
		} catch (FileNotFoundException e) {
			System.err.println("No such file" + inputFile.getAbsolutePath());
		}
		BufferedReader bufferedFileReader = new BufferedReader(inputFileReader);
		BufferedWriter buffwrite = null;
		buffwrite = new BufferedWriter(
				new FileWriter(stat + week + "clean.txt"));
		String lineOfInput = null;
		try {
			while (((lineOfInput = bufferedFileReader.readLine()) != null)) {
				lineOfInput = lineOfInput.replace(
						" <a href=\"/nfl/players/playerpage/", ",");
				lineOfInput = lineOfInput.replace("</td><td>", ",");
				lineOfInput = lineOfInput.replace("<td align=\"left\">", ",");
				lineOfInput = lineOfInput.replace(
						"</td>,<span class=\"greenfont\">", ",");
				lineOfInput = lineOfInput.replace(
						"</td>,<span class=\"redfont\">", ",");
				lineOfInput = lineOfInput.replace(
						"</span> <a href=\"/nfl/boxscore?gameId=", ",");
				lineOfInput = lineOfInput.replace(
						"</td><td class=\"sortcell\">", ",");
				lineOfInput = lineOfInput.replace("</td></tr>", "");
				lineOfInput = lineOfInput.replace("</a> ", "");
				lineOfInput = lineOfInput.replace("</a>", "");
				lineOfInput = lineOfInput.replace("</td>", "");
				lineOfInput = lineOfInput.replace("\">", ",");
				lineOfInput = lineOfInput.replace(
						"<tr class=\"row1\" align=\"right,,", "");
				lineOfInput = lineOfInput.replace(
						"<br><td align=\"center,<a href=\"/nfl/teams/page/",
						",");
				lineOfInput = lineOfInput.replace("             ", "");
				lineOfInput = lineOfInput.replace("  ", "");
				if (lineOfInput.startsWith(" ")) {
					lineOfInput = lineOfInput.substring(1);
				}
				lineOfInput = lineOfInput.replace(".,", ",");
				if ((lineOfInput.startsWith("1") || lineOfInput.startsWith("2")
						|| lineOfInput.startsWith("3")
						|| lineOfInput.startsWith("4")
						|| lineOfInput.startsWith("5")
						|| lineOfInput.startsWith("6")
						|| lineOfInput.startsWith("7")
						|| lineOfInput.startsWith("8") || lineOfInput
							.startsWith("9"))) {
					String[] tokens = lineOfInput.split(",");
					int counter = 0;
					for (String token : tokens) {
						if (!(counter == 1 || counter == 4)
								|| counter == tokens.length - 1) {
							buffwrite.append(token + ",");
							// System.out.println(token+",");
						}
						if (counter == tokens.length - 1) {
							buffwrite.append(token);
							// System.out.println(token);
						}
						counter++;
					}
					// System.out.println("");
					buffwrite.append("\n");
				}
			}
		} finally {
			bufferedFileReader.close();
			inputFileReader.close();
			if (buffwrite != null) {
				try {
					buffwrite.flush();
					buffwrite.close();
				} catch (IOException ioe) {
					ioe.printStackTrace();
				}
			}
		}
	}

	public static void readDef() throws IOException {
		//System.out.println("Starting to parse def.txt");
		File inputFile = new File("def.txt");
		FileReader inputFileReader = null;
		try {

			inputFileReader = new FileReader(inputFile);
		} catch (FileNotFoundException e) {
			System.err.println("No such file" + inputFile.getAbsolutePath());
		}
		BufferedReader bufferedFileReader = new BufferedReader(inputFileReader);
		BufferedWriter buffwrite = null;
		buffwrite = new BufferedWriter(new FileWriter(
				"defclean.txt"));
		String lineOfInput = null;
		try {
			for (int x = 0; x < 312; x++)
				bufferedFileReader.readLine();
			int weekCounter = 1;
			while (((lineOfInput = bufferedFileReader.readLine()) != null)) {
				if (lineOfInput.contains("Week ")) {
					buffwrite.append(":Week " + weekCounter);
					buffwrite.append("\n");
					weekCounter++;
				}
				if (lineOfInput.contains("<a href=\"/nfl/boxscore?gameId=")){
					lineOfInput = lineOfInput.replace("<tr class=\"oddrow team-28-", "");
					lineOfInput = lineOfInput.replace("<tr class=\"evenrow team-28-", "");
					lineOfInput = lineOfInput.replace(" team-28-", "");
					lineOfInput = lineOfInput.replace("\"><td nowrap colspan=\"2\"><a href=\"/nfl/boxscore?gameId=", "");
					lineOfInput = lineOfInput.replace("\">", ",");
					lineOfInput = lineOfInput.replace("</a></td>", "");
					lineOfInput = teamComma(lineOfInput);
					lineOfInput = lineOfInput.replace(" (OT)", "");
					buffwrite.append(lineOfInput);
					buffwrite.append("\n");
				}
			}
		} finally {
			bufferedFileReader.close();
			inputFileReader.close();
			if (buffwrite != null) {
				try {
					buffwrite.flush();
					buffwrite.close();
				} catch (IOException ioe) {
					ioe.printStackTrace();
				}
			}
		}

	}
	
	public static String teamComma(String line){
		String result = line;
		if (line.contains("Arizona "))
			result = result.replace("Arizona ", "Arizona,");
		if (line.contains("Atlanta "))
			result = result.replace("Atlanta ", "Atlanta,");
		if (line.contains("Baltimore "))
			result = result.replace("Baltimore ", "Baltimore,");
		if (line.contains("Buffalo "))
			result = result.replace("Buffalo ", "Buffalo,");
		if (line.contains("Carolina "))
			result = result.replace("Carolina ", "Carolina,");
		if (line.contains("Chicago "))
			result = result.replace("Chicago ", "Chicago,");
		if (line.contains("Cincinnati "))
			result = result.replace("Cincinnati ", "Cincinnati,");
		if (line.contains("Cleveland "))
			result = result.replace("Cleveland ", "Cleveland,");
		if (line.contains("Dallas "))
			result = result.replace("Dallas ", "Dallas,");
		if (line.contains("Denver "))
			result = result.replace("Denver ", "Denver,");
		if (line.contains("Detroit "))
			result = result.replace("Detroit ", "Detroit,");
		if (line.contains("Green Bay "))
			result = result.replace("Green Bay ", "Green Bay,");
		if (line.contains("Houston "))
			result = result.replace("Houston ", "Houston,");
		if (line.contains("Indianapolis "))
			result = result.replace("Indianapolis ", "Indianapolis,");
		if (line.contains("Jacksonville "))
			result = result.replace("Jacksonville ", "Jacksonville,");
		if (line.contains("Kansas City "))
			result = result.replace("Kansas City ", "Kansas City,");
		if (line.contains("Miami "))
			result = result.replace("Miami ", "Miami,");
		if (line.contains("Minnesota "))
			result = result.replace("Minnesota ", "Minnesota,");
		if (line.contains("New England "))
			result = result.replace("New England ", "New England,");
		if (line.contains("New Orleans "))
			result = result.replace("New Orleans ", "New Orleans,");
		if (line.contains("NY Giants "))
			result = result.replace("NY Giants ", "NY Giants,");
		if (line.contains("NY Jets "))
			result = result.replace("NY Jets ", "NY Jets,");
		if (line.contains("Oakland "))
			result = result.replace("Oakland ", "Oakland,");
		if (line.contains("Philadelphia "))
			result = result.replace("Philadelphia ", "Philadelphia,");		
		if (line.contains("Pittsburgh "))
				result = result.replace("Pittsburgh ", "Pittsburgh,");
		if (line.contains("San Diego "))
				result = result.replace("San Diego ", "San Diego,");
		if (line.contains("San Francisco "))
			result = result.replace("San Francisco ", "San Francisco,");
		if (line.contains("Seattle "))
			result = result.replace("Seattle ", "Seattle,");
		if (line.contains("St. Louis "))
			result = result.replace("St. Louis ", "St. Louis,");
		if (line.contains("Tampa Bay "))
			result = result.replace("Tampa Bay ", "Tampa Bay,");
		if (line.contains("Tennessee "))
			result = result.replace("Tennessee ", "Tennessee,");
		if (line.contains("Washington "))
			result = result.replace("Washington ", "Washington,");
		return result;
	}

}
